package com.ymatou.iapi.couponservice.testcase;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.couponservice.parameter.ReturnCouponBackBean;
import com.ymatou.iapi.couponservice.parameter.UseCouponBean;
import com.ymatou.iapi.couponservice.parameter.UseCouponCallbackBean;
import com.ymatou.iapi.couponservice.parameter.UseCouponInfo;
import com.ymatou.iapi.couponservice.service.ReturnCouponBackCall;
import com.ymatou.iapi.couponservice.service.UseCouponCallbackCall;
import com.ymatou.iapi.couponservice.service.UserReceiveCouponCall;
import com.ymatou.iapi.m.couponservice.parameter.CreateCouponBean;
import com.ymatou.iapi.m.couponservice.service.CreateCouponCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponServiceCallService;
import com.ymttest.business.service.CouponServiceMCallService;
import com.ymttest.database.model.YmtCouponBatch;
import com.ymttest.database.model.YmtCouponPrivateUserBound;
import com.ymttest.database.model.YmtCouponSetting;
import com.ymttest.database.model.YmtCouponTradeRecord;
import com.ymttest.database.sqlwapper.YmtCouponBatchWapper;
import com.ymttest.database.sqlwapper.YmtCouponPrivateUserBoundWapper;
import com.ymttest.database.sqlwapper.YmtCouponSettingWapper;
import com.ymttest.database.sqlwapper.YmtCouponTradeRecordWapper;
import com.ymttest.utils.TestDataManager;
/**
*还券
*FreeMaker Template自动生成代码
*<!--添加还券记录-->
		insert into ymt_CouponTradeRecord (OrderId, UserId, CouponCode, BatchId, TradeType)
		values (#{orderId,jdbcType=INTEGER},
		#{userId,jdbcType=INTEGER},
		#{couponCode,jdbcType=VARCHAR},
		#{batchId,jdbcType=INTEGER},
		#{tradeType,jdbcType=TINYINT})

		<!--增加用户优惠券使用剩余次数-->
		UPDATE a SET iCouponUsedCount=iCouponUsedCount+1 from Ymt_CouponPrivateUserBound a with (index  = IX_Ymt_CouponPrivateUserBound_sCouponCode)
		WHERE sCouponCode=#{couponCode, jdbcType=VARCHAR}
		AND iUserId=#{userId, jdbcType=INTEGER}

		<!--更新批次优惠券剩余使用次数-->
		UPDATE Ymt_CouponSetting SET iMaxUseTime=iMaxUseTime+1
		WHERE iCouponSettingId IN (SELECT iCouponSettingId FROM Ymt_CouponBatch WITH(NOLOCK) WHERE ibatchid=#{batchId, jdbcType=INTEGER})
		
		业务逻辑和用户一样：1：ymt_couponsetting 为0,立即更新  2：Ymt_CouponPrivateUserBound mainOrderId 不变  3:Ymt_CouponPrivateUserBound iCouponUsedCount=1，表示有1次使用次数  IsUpdateUsedCount=false
		返回字符串 “ok”，“fail”
*/
@TestSuite
public class Ts_ReturnCouponBackCoupon{
	private static ReturnCouponBackBean ReturnCouponBackBean;
	private static ReturnCouponBackCall ReturnCouponBackCall;
	
	private static UseCouponCallbackBean usecouponcallbackBean;
	private static UseCouponCallbackCall usecouponcallbackCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("还券");
	}

	@Before
	public void caseUp() {
		ReturnCouponBackBean=new ReturnCouponBackBean();
		ReturnCouponBackCall=new ReturnCouponBackCall();
		
		usecouponcallbackBean=new UseCouponCallbackBean();
		usecouponcallbackCall=new UseCouponCallbackCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(ReturnCouponBackCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReturnCouponBack_001() {
		Logger.start(true,"还券-平台券-领取券");
		try {
			
			int userId = TestDataManager.getRandomIntNum(8);
			int orderId = TestDataManager.getRandomIntNum(10);
			
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			CouponServiceCallService couponServiceCallService = new CouponServiceCallService();
			
			CreateCouponBean createCouponBean = couponServiceMCallService.setDefaultPlatformCreateCouponBeanForRecieve();
			CreateCouponCall createCouponCall= couponServiceMCallService.createPlatformCoupon(createCouponBean);
			UserReceiveCouponCall userReceiveCouponCall = couponServiceMCallService.receiveCouponInfo(createCouponCall.getBatchCode(), userId);
			
			String couponCode = userReceiveCouponCall.getUserReceiveResult().getCouponCode();
			
			YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(createCouponCall.getBatchCode()).get(0);
			YmtCouponSetting setting1 = new YmtCouponSettingWapper().selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
			YmtCouponPrivateUserBound privateUserBound1 = new YmtCouponPrivateUserBoundWapper().selectBysCouponCode(userReceiveCouponCall.getUserReceiveResult().getCouponCode()).get(0);
			
			UseCouponBean useCouponBean = couponServiceCallService.setUseCouponBean(userId, orderId, couponCode);
			couponServiceCallService.useCoupon(useCouponBean);
			
			//更改使用剩余次数 使用后减1
          usecouponcallbackBean = couponServiceCallService.setUseCouponCallbackBean(orderId, null, userReceiveCouponCall.getUserReceiveResult().getCouponCode());
			usecouponcallbackCall.setData(usecouponcallbackBean);
			usecouponcallbackCall.callService();
			
			List<YmtCouponTradeRecord> ymtCouponTradeRecords2 = new YmtCouponTradeRecordWapper().selectByOrderIdAndCouponCode(orderId,couponCode);			
			YmtCouponPrivateUserBound privateUserBound2 = new YmtCouponPrivateUserBoundWapper().selectBysCouponCode(userReceiveCouponCall.getUserReceiveResult().getCouponCode()).get(0);
			YmtCouponSetting setting2 = new YmtCouponSettingWapper().selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
			
			List<String> CouponCodes=new ArrayList();
			CouponCodes.add(couponCode);
			ReturnCouponBackBean.setUserId(userId);
			ReturnCouponBackBean.setMainOrderId(orderId);
			ReturnCouponBackBean.setCouponCodes(CouponCodes);
			
			ReturnCouponBackBean.setAppId("app");
			ReturnCouponBackBean.setRequestId("1233344");			
			
			ReturnCouponBackCall.setData(ReturnCouponBackBean);
			ReturnCouponBackCall.callService();		
			
			Logger.verifyEquals("ok", ReturnCouponBackCall.getData(), "验证返回ok");
					
			YmtCouponPrivateUserBound privateUserBound3 = new YmtCouponPrivateUserBoundWapper().selectBysCouponCode(userReceiveCouponCall.getUserReceiveResult().getCouponCode()).get(0);
			YmtCouponSetting setting3 = new YmtCouponSettingWapper().selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
			
			Logger.verifyEquals(1, privateUserBound1.getiCouponUsedCount()-privateUserBound2.getiCouponUsedCount(), "验证数据库Ymt_CouponPrivateUserBound中iCouponUsedCount");
			Logger.verifyEquals(1, setting1.getiMaxUseTime()-setting2.getiMaxUseTime(), "验证数据库Ymt_CouponSetting中iMaxUseTime");

			Logger.verifyEquals(privateUserBound3.getiCouponUsedCount(), privateUserBound1.getiCouponUsedCount(), "验证数据库Ymt_CouponPrivateUserBound中iCouponUsedCount");
			Logger.verifyEquals(setting3.getiMaxUseTime(), setting1.getiMaxUseTime(), "验证数据库Ymt_CouponSetting中iMaxUseTime");

			Logger.verifyEquals(1, privateUserBound3.getiCouponUsedCount(), "验证数据库Ymt_CouponPrivateUserBound返券iCouponUsedCount为1");
			Logger.verifyEquals(false, privateUserBound3.getIsUpdateUsedCount(), "验证数据库Ymt_CouponPrivateUserBound返券IsUpdateUsedCount为0");
			
//			Logger.verifyEquals(1, ymtCouponTradeRecords2.size(), "验证生成YmtCouponTradeRecord记录数");
//			Logger.verifyEquals(2, ymtCouponTradeRecords3.size(), "验证生成YmtCouponTradeRecord记录数");
//
//			YmtCouponTradeRecord ymtCouponTradeRecord = ymtCouponTradeRecords3.get(0);
//			Logger.verifyEquals(batch.getiBatchId(), ymtCouponTradeRecord.getBatchId(), "验证数据库ymt_CouponTradeRecord中iBatchId");
//			Logger.verifyEquals(1, ymtCouponTradeRecord.getTradeType().intValue(), "验证数据库ymt_CouponTradeRecord中TradeType:1:创建订单  2:取消订单");			
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReturnCouponBack_002() {
		Logger.start(true,"还券-商家券");
		try {
			
			int userId = TestDataManager.getRandomIntNum(8);
			int orderId = TestDataManager.getRandomIntNum(10);
			
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			CouponServiceCallService couponServiceCallService = new CouponServiceCallService();
			
			CreateCouponBean createCouponBean = couponServiceMCallService.setDefaultPlatformCreateCouponBeanForRecieve();
			CreateCouponCall createCouponCall= couponServiceMCallService.createPlatformCoupon(createCouponBean);
			UserReceiveCouponCall userReceiveCouponCall = couponServiceMCallService.receiveCouponInfo(createCouponCall.getBatchCode(), userId);
			
			String couponCode = userReceiveCouponCall.getUserReceiveResult().getCouponCode();
			
			YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(createCouponCall.getBatchCode()).get(0);
			YmtCouponSetting setting1 = new YmtCouponSettingWapper().selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
			YmtCouponPrivateUserBound privateUserBound1 = new YmtCouponPrivateUserBoundWapper().selectBysCouponCode(userReceiveCouponCall.getUserReceiveResult().getCouponCode()).get(0);
			
			UseCouponBean useCouponBean = couponServiceCallService.setUseCouponBean(userId, orderId, couponCode);
			couponServiceCallService.useCoupon(useCouponBean);
			
			//更改使用剩余次数 使用后减1
		   usecouponcallbackBean = couponServiceCallService.setUseCouponCallbackBean(orderId, null, userReceiveCouponCall.getUserReceiveResult().getCouponCode());
		   usecouponcallbackCall.setData(usecouponcallbackBean);
		   usecouponcallbackCall.callService();
			
			List<YmtCouponTradeRecord> ymtCouponTradeRecords2 = new YmtCouponTradeRecordWapper().selectByOrderIdAndCouponCode(orderId,couponCode);			
			YmtCouponPrivateUserBound privateUserBound2 = new YmtCouponPrivateUserBoundWapper().selectBysCouponCode(userReceiveCouponCall.getUserReceiveResult().getCouponCode()).get(0);
			YmtCouponSetting setting2 = new YmtCouponSettingWapper().selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
			
			List<String> CouponCodes=new ArrayList();
			CouponCodes.add(couponCode);
			ReturnCouponBackBean.setUserId(userId);
			ReturnCouponBackBean.setMainOrderId(orderId);
			ReturnCouponBackBean.setCouponCodes(CouponCodes);
			ReturnCouponBackBean.setAppId("app");
			ReturnCouponBackBean.setRequestId("1233344");			
			
			ReturnCouponBackCall.setData(ReturnCouponBackBean);
			ReturnCouponBackCall.callService();		
			
			Logger.verifyEquals("ok", ReturnCouponBackCall.getData(), "验证返回ok");
			
			List<YmtCouponTradeRecord> ymtCouponTradeRecords3 = new YmtCouponTradeRecordWapper().selectByOrderIdAndCouponCode(orderId,couponCode);			
			YmtCouponPrivateUserBound privateUserBound3 = new YmtCouponPrivateUserBoundWapper().selectBysCouponCode(userReceiveCouponCall.getUserReceiveResult().getCouponCode()).get(0);
			YmtCouponSetting setting3 = new YmtCouponSettingWapper().selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
			
			Logger.verifyEquals(1, privateUserBound1.getiCouponUsedCount()-privateUserBound2.getiCouponUsedCount(), "验证数据库Ymt_CouponPrivateUserBound中iCouponUsedCount");
			Logger.verifyEquals(1, setting1.getiMaxUseTime()-setting2.getiMaxUseTime(), "验证数据库Ymt_CouponSetting中iMaxUseTime");

			Logger.verifyEquals(privateUserBound3.getiCouponUsedCount(), privateUserBound1.getiCouponUsedCount(), "验证数据库Ymt_CouponPrivateUserBound中iCouponUsedCount");
			Logger.verifyEquals(setting3.getiMaxUseTime(), setting1.getiMaxUseTime(), "验证数据库Ymt_CouponSetting中iMaxUseTime");
			Logger.verifyEquals(1, privateUserBound3.getiCouponUsedCount(), "验证数据库Ymt_CouponPrivateUserBound返券iCouponUsedCount为1");
			Logger.verifyEquals(false, privateUserBound3.getIsUpdateUsedCount(), "验证数据库Ymt_CouponPrivateUserBound返券IsUpdateUsedCount为0");

//			Logger.verifyEquals(1, ymtCouponTradeRecords2.size(), "验证生成YmtCouponTradeRecord记录数");
//			Logger.verifyEquals(2, ymtCouponTradeRecords3.size(), "验证生成YmtCouponTradeRecord记录数");
//
//			YmtCouponTradeRecord ymtCouponTradeRecord = ymtCouponTradeRecords3.get(0);
//			Logger.verifyEquals(batch.getiBatchId(), ymtCouponTradeRecord.getBatchId(), "验证数据库ymt_CouponTradeRecord中iBatchId");
//			Logger.verifyEquals(1, ymtCouponTradeRecord.getTradeType().intValue(), "验证数据库ymt_CouponTradeRecord中TradeType:1:创建订单  2:取消订单");			
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReturnCouponBack_003() {
		Logger.start(true,"还券-OrderId空");
		try {
			
			int userId = TestDataManager.getRandomIntNum(8);
			int orderId = TestDataManager.getRandomIntNum(10);
			
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			CouponServiceCallService couponServiceCallService = new CouponServiceCallService();
			
			CreateCouponBean createCouponBean = couponServiceMCallService.setDefaultPlatformCreateCouponBeanForRecieve();
			CreateCouponCall createCouponCall= couponServiceMCallService.createPlatformCoupon(createCouponBean);
			UserReceiveCouponCall userReceiveCouponCall = couponServiceMCallService.receiveCouponInfo(createCouponCall.getBatchCode(), userId);
			
			String couponCode = userReceiveCouponCall.getUserReceiveResult().getCouponCode();
			
			YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(createCouponCall.getBatchCode()).get(0);
			YmtCouponSetting setting1 = new YmtCouponSettingWapper().selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
			YmtCouponPrivateUserBound privateUserBound1 = new YmtCouponPrivateUserBoundWapper().selectBysCouponCode(userReceiveCouponCall.getUserReceiveResult().getCouponCode()).get(0);
			
			UseCouponBean useCouponBean = couponServiceCallService.setUseCouponBean(userId, orderId, couponCode);
			couponServiceCallService.useCoupon(useCouponBean);
			
			//更改使用剩余次数 使用后减1
		   usecouponcallbackBean = couponServiceCallService.setUseCouponCallbackBean(orderId, null, userReceiveCouponCall.getUserReceiveResult().getCouponCode());
		   usecouponcallbackCall.setData(usecouponcallbackBean);
		   usecouponcallbackCall.callService();
			   
			List<YmtCouponTradeRecord> ymtCouponTradeRecords2 = new YmtCouponTradeRecordWapper().selectByOrderIdAndCouponCode(orderId,couponCode);			
			YmtCouponPrivateUserBound privateUserBound2 = new YmtCouponPrivateUserBoundWapper().selectBysCouponCode(userReceiveCouponCall.getUserReceiveResult().getCouponCode()).get(0);
			YmtCouponSetting setting2 = new YmtCouponSettingWapper().selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
			
			List<String> CouponCodes=new ArrayList();
			CouponCodes.add(couponCode);
			ReturnCouponBackBean.setUserId(userId);
			//ReturnCouponBackBean.setMainOrderId(orderId);
			ReturnCouponBackBean.setCouponCodes(CouponCodes);
			ReturnCouponBackBean.setAppId("app");
			ReturnCouponBackBean.setRequestId("1233344");			
			
			ReturnCouponBackCall.setData(ReturnCouponBackBean);
			ReturnCouponBackCall.callService();		
			
			Logger.verifyEquals("fail", ReturnCouponBackCall.getData(), "验证返回fail");
			
			List<YmtCouponTradeRecord> ymtCouponTradeRecords3 = new YmtCouponTradeRecordWapper().selectByOrderIdAndCouponCode(orderId,couponCode);			
			YmtCouponPrivateUserBound privateUserBound3 = new YmtCouponPrivateUserBoundWapper().selectBysCouponCode(userReceiveCouponCall.getUserReceiveResult().getCouponCode()).get(0);
			YmtCouponSetting setting3 = new YmtCouponSettingWapper().selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
			
			Logger.verifyEquals(1, privateUserBound1.getiCouponUsedCount()-privateUserBound2.getiCouponUsedCount(), "验证数据库Ymt_CouponPrivateUserBound中iCouponUsedCount");
			Logger.verifyEquals(1, setting1.getiMaxUseTime()-setting2.getiMaxUseTime(), "验证数据库Ymt_CouponSetting中iMaxUseTime");

//			Logger.verifyEquals(1, ymtCouponTradeRecords2.size(), "验证生成YmtCouponTradeRecord记录数");
//			Logger.verifyEquals(1, ymtCouponTradeRecords3.size(), "验证生成YmtCouponTradeRecord记录数");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReturnCouponBack_004() {
		Logger.start(true,"还券-CouponCode空");
		try {
			
			int userId = TestDataManager.getRandomIntNum(8);
			int orderId = TestDataManager.getRandomIntNum(10);
			
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			CouponServiceCallService couponServiceCallService = new CouponServiceCallService();
			
			CreateCouponBean createCouponBean = couponServiceMCallService.setDefaultPlatformCreateCouponBeanForRecieve();
			CreateCouponCall createCouponCall= couponServiceMCallService.createPlatformCoupon(createCouponBean);
			UserReceiveCouponCall userReceiveCouponCall = couponServiceMCallService.receiveCouponInfo(createCouponCall.getBatchCode(), userId);
			
			String couponCode = userReceiveCouponCall.getUserReceiveResult().getCouponCode();
			
			YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(createCouponCall.getBatchCode()).get(0);
			YmtCouponSetting setting1 = new YmtCouponSettingWapper().selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
			YmtCouponPrivateUserBound privateUserBound1 = new YmtCouponPrivateUserBoundWapper().selectBysCouponCode(userReceiveCouponCall.getUserReceiveResult().getCouponCode()).get(0);
			
			UseCouponBean useCouponBean = couponServiceCallService.setUseCouponBean(userId, orderId, couponCode);
			couponServiceCallService.useCoupon(useCouponBean);
			
			//更改使用剩余次数 使用后减1
			   usecouponcallbackBean = couponServiceCallService.setUseCouponCallbackBean(orderId, null, userReceiveCouponCall.getUserReceiveResult().getCouponCode());
			   usecouponcallbackCall.setData(usecouponcallbackBean);
			   usecouponcallbackCall.callService();
			
			List<YmtCouponTradeRecord> ymtCouponTradeRecords2 = new YmtCouponTradeRecordWapper().selectByOrderIdAndCouponCode(orderId,couponCode);			
			YmtCouponPrivateUserBound privateUserBound2 = new YmtCouponPrivateUserBoundWapper().selectBysCouponCode(userReceiveCouponCall.getUserReceiveResult().getCouponCode()).get(0);
			YmtCouponSetting setting2 = new YmtCouponSettingWapper().selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
			
			List<String> CouponCodes=new ArrayList();
			ReturnCouponBackBean.setUserId(userId);
			ReturnCouponBackBean.setMainOrderId(orderId);
			ReturnCouponBackBean.setCouponCodes(CouponCodes);
			ReturnCouponBackBean.setAppId("app");
			ReturnCouponBackBean.setRequestId("1233344");			
			
			ReturnCouponBackCall.setData(ReturnCouponBackBean);
			ReturnCouponBackCall.callService();		
			
			Logger.verifyEquals("fail", ReturnCouponBackCall.getData(), "验证返回fail");
			
			List<YmtCouponTradeRecord> ymtCouponTradeRecords3 = new YmtCouponTradeRecordWapper().selectByOrderIdAndCouponCode(orderId,couponCode);			
			YmtCouponPrivateUserBound privateUserBound3 = new YmtCouponPrivateUserBoundWapper().selectBysCouponCode(userReceiveCouponCall.getUserReceiveResult().getCouponCode()).get(0);
			YmtCouponSetting setting3 = new YmtCouponSettingWapper().selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
			
			Logger.verifyEquals(1, privateUserBound1.getiCouponUsedCount()-privateUserBound2.getiCouponUsedCount(), "验证数据库Ymt_CouponPrivateUserBound中iCouponUsedCount");
			Logger.verifyEquals(1, setting1.getiMaxUseTime()-setting2.getiMaxUseTime(), "验证数据库Ymt_CouponSetting中iMaxUseTime");

//			Logger.verifyEquals(1, ymtCouponTradeRecords2.size(), "验证生成YmtCouponTradeRecord记录数");
//			Logger.verifyEquals(1, ymtCouponTradeRecords3.size(), "验证生成YmtCouponTradeRecord记录数");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReturnCouponBack_005() {
		Logger.start(true,"还券-OrderId与CouponCode不匹配");
		try {
			
			int userId = TestDataManager.getRandomIntNum(8);
			int orderId = TestDataManager.getRandomIntNum(10);
			
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			CouponServiceCallService couponServiceCallService = new CouponServiceCallService();
			
			CreateCouponBean createCouponBean = couponServiceMCallService.setDefaultPlatformCreateCouponBeanForRecieve();
			CreateCouponCall createCouponCall= couponServiceMCallService.createPlatformCoupon(createCouponBean);
			UserReceiveCouponCall userReceiveCouponCall = couponServiceMCallService.receiveCouponInfo(createCouponCall.getBatchCode(), userId);
			
			String couponCode = userReceiveCouponCall.getUserReceiveResult().getCouponCode();
			
			YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(createCouponCall.getBatchCode()).get(0);
			YmtCouponSetting setting1 = new YmtCouponSettingWapper().selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
			YmtCouponPrivateUserBound privateUserBound1 = new YmtCouponPrivateUserBoundWapper().selectBysCouponCode(userReceiveCouponCall.getUserReceiveResult().getCouponCode()).get(0);
			
			UseCouponBean useCouponBean = couponServiceCallService.setUseCouponBean(userId, orderId, couponCode);
			couponServiceCallService.useCoupon(useCouponBean);
			
			//更改使用剩余次数 使用后减1
			   usecouponcallbackBean = couponServiceCallService.setUseCouponCallbackBean(orderId, null, userReceiveCouponCall.getUserReceiveResult().getCouponCode());
			   usecouponcallbackCall.setData(usecouponcallbackBean);
			   usecouponcallbackCall.callService();
			   
			List<YmtCouponTradeRecord> ymtCouponTradeRecords2 = new YmtCouponTradeRecordWapper().selectByOrderIdAndCouponCode(orderId,couponCode);			
			YmtCouponPrivateUserBound privateUserBound2 = new YmtCouponPrivateUserBoundWapper().selectBysCouponCode(userReceiveCouponCall.getUserReceiveResult().getCouponCode()).get(0);
			YmtCouponSetting setting2 = new YmtCouponSettingWapper().selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
			
			List<String> CouponCodes=new ArrayList();
			CouponCodes.add(couponCode);
			ReturnCouponBackBean.setUserId(userId);
			ReturnCouponBackBean.setMainOrderId(orderId+1);
			ReturnCouponBackBean.setCouponCodes(CouponCodes);
			ReturnCouponBackBean.setAppId("app");
			ReturnCouponBackBean.setRequestId("1233344");			
			
			ReturnCouponBackCall.setData(ReturnCouponBackBean);
			ReturnCouponBackCall.callService();		
			
			Logger.verifyEquals("fail", ReturnCouponBackCall.getData(), "验证返回fail");
			
			List<YmtCouponTradeRecord> ymtCouponTradeRecords3 = new YmtCouponTradeRecordWapper().selectByOrderIdAndCouponCode(orderId,couponCode);			
			YmtCouponPrivateUserBound privateUserBound3 = new YmtCouponPrivateUserBoundWapper().selectBysCouponCode(userReceiveCouponCall.getUserReceiveResult().getCouponCode()).get(0);
			YmtCouponSetting setting3 = new YmtCouponSettingWapper().selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
			
			Logger.verifyEquals(1, privateUserBound1.getiCouponUsedCount()-privateUserBound2.getiCouponUsedCount(), "验证数据库Ymt_CouponPrivateUserBound中iCouponUsedCount");
			Logger.verifyEquals(1, setting1.getiMaxUseTime()-setting2.getiMaxUseTime(), "验证数据库Ymt_CouponSetting中iMaxUseTime");

//			Logger.verifyEquals(1, ymtCouponTradeRecords2.size(), "验证生成YmtCouponTradeRecord记录数");
//			Logger.verifyEquals(1, ymtCouponTradeRecords3.size(), "验证生成YmtCouponTradeRecord记录数");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}